<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 sdtc xsi exsl">

	<!-- Encounter in 卫生事件章节 -->
	<xsl:template match="*" mode="EventEncounter">
		<xsl:variable name="encounterType"><xsl:value-of select="hl7:entry/hl7:observation[hl7:code/@code='DE02.01.060.00']/hl7:value/@displayName"/></xsl:variable>
		<xsl:variable name="fromTime"><xsl:value-of select="isc:evaluate('xmltimestamp', hl7:entry/hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE06.00.092.00']/hl7:value)"/></xsl:variable>
		<xsl:variable name="toTime"><xsl:value-of select="isc:evaluate('xmltimestamp',hl7:entry/hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE06.00.017.00']/hl7:value)"/></xsl:variable>
		
		<Encounters>
			<Encounter>
			    <xsl:choose>
					<xsl:when test="$encounterType='住院'">
						<EncounterType><xsl:value-of select="'I'"/></EncounterType>
					</xsl:when>
					<xsl:when test="$encounterType='门诊'">
						<EncounterType><xsl:value-of select="'O'"/></EncounterType>
					</xsl:when>
					<xsl:when test="$encounterType='急诊'">
						<EncounterType><xsl:value-of select="'E'"/></EncounterType>
					</xsl:when>
					<xsl:otherwise>
						<EncounterType><xsl:value-of select="'O'"/></EncounterType>
					</xsl:otherwise>
				</xsl:choose>
				
				<xsl:choose>
					<xsl:when test="$encounterType='住院'">
						<EncounterNumber><xsl:value-of select="hl7:entry/hl7:observation[hl7:code/@code='DE01.00.014.00']/hl7:value"/></EncounterNumber>
					</xsl:when>
					<xsl:otherwise>
						<EncounterNumber><xsl:value-of select="hl7:entry/hl7:observation[hl7:code/@code='DE01.00.010.00']/hl7:value"/></EncounterNumber>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:if test="string-length($fromTime)">
				<FromTime><xsl:value-of select="$fromTime"/></FromTime>
				</xsl:if>
				<xsl:if test="string-length($toTime)">
				<ToTime><xsl:value-of select="$toTime"/></ToTime>
				<EndTime><xsl:value-of select="$toTime"/></EndTime>
				</xsl:if>
				<Locations>
					<Location>
						<LocationDetail>
							<Code><xsl:value-of select="hl7:entry/hl7:observation[hl7:code/@code='DE08.10.026.00']/hl7:value"/></Code>
							<Description><xsl:value-of select="hl7:entry/hl7:observation[hl7:code/@code='DE08.10.026.00']/hl7:value"/></Description>
						</LocationDetail>
					</Location>
				</Locations>
				<xsl:apply-templates select="." mode="HCC-HealthCareFacility"/>
				<!-- Custom SDA Data-->
				<xsl:apply-templates select="." mode="ImportCustom-Encounter"/>
			</Encounter>
		</Encounters>
	</xsl:template>
	
	<!-- Encounter in providerOrganization with less or more details -->
	<xsl:template match="*" mode="OtherEncounter">
		<xsl:variable name="fromTime"><xsl:value-of select="isc:evaluate('xmltimestamp',/hl7:ClinicalDocument/hl7:author/hl7:time/@value)"/></xsl:variable>
		<Encounters>
			<Encounter>
				<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
				
				<xsl:if test="string-length($fromTime)">
				<FromTime><xsl:value-of select="$fromTime"/></FromTime>
				</xsl:if>

				<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HospitalDischargeDX]/hl7:entry/hl7:observation[hl7:code/@code='DE06.00.223.00']" mode="HCCASeparationMode"/>
				
				<xsl:choose>
					<xsl:when test="($hccDocType='C0002') or ($hccDocType='C0003') or ($hccDocType='C0004') or ($hccDocType='C0005')">
						<xsl:choose>
							<xsl:when test="$hccDocType='C0003'">
								<EncounterType><xsl:value-of select="'E'"/></EncounterType>
							</xsl:when>
							<xsl:otherwise>
								<EncounterType><xsl:value-of select="'O'"/></EncounterType>
							</xsl:otherwise>
						</xsl:choose>
						<Locations>
							<Location>
								<LocationDetail>
									<Code><xsl:value-of select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:providerOrganization/hl7:id/@extension"/></Code>
									<Description><xsl:value-of select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:providerOrganization/hl7:name"/></Description>
								</LocationDetail>
							</Location>
						</Locations>
					</xsl:when>
					<xsl:otherwise>
						<EncounterType><xsl:value-of select="'I'"/></EncounterType>
						<xsl:apply-templates select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:providerOrganization/hl7:asOrganizationPartOf/hl7:wholeOrganization" mode="HCCLocation"/>
					</xsl:otherwise>
				</xsl:choose>	
				<xsl:apply-templates select="." mode="HCC-HealthCareFacility"/>
				<!-- Custom SDA Data-->
				<xsl:apply-templates select="." mode="ImportCustom-Encounter"/>
			</Encounter>
		</Encounters>

	</xsl:template>
	
	
	<!-- DefaultEncounter imports the HCC encompassingEncounter. -->
	<xsl:template match="*" mode="DefaultEncounter">
		<!--
			$encounterIDTemp is used as an intermediate so that
			$encounterID can be set such that "000nnn" does NOT
			match "nnn" when comparing encounter numbers.
		-->
		<xsl:variable name="encounterIDTemp"><xsl:apply-templates select="." mode="EncounterId"/></xsl:variable>
		<xsl:variable name="encounterID" select="string($encounterIDTemp)"/>
		
		<Encounters>
			<Encounter>
				<xsl:apply-templates select="." mode="DefaultEncounterDetail"/>
				
				<!-- Visit reason -->
				<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_AdmissionInfo]/hl7:entry/hl7:observation[hl7:code/@code='DE05.10.053.00']" mode="HCCVisitReason"/>
				<!-- Admission Source -->
				<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_AdmissionInfo]/hl7:entry/hl7:observation[hl7:code/@code='DE06.00.339.00']" mode="EncounterAdmissionSource"/>
				<!-- Separation Mode -->
				<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HospitalDischargeDX]/hl7:entry/hl7:observation[hl7:code/@code='DE06.00.223.00']" mode="HCCASeparationMode"/>
				<!-- Visit Description 住院过程章节的诊疗过程-->
				<!--<xsl:apply-templates select="/hl7:ClinicalDocument" mode="ReasonForVisitSection"/>-->
				<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HospitalCourse]/hl7:entry/hl7:observation[hl7:code/@code='DE06.00.296.00']" mode="HCCVisitDescription"/>
				
				<!-- Custom SDA Data-->
				<xsl:apply-templates select="." mode="ImportCustom-Encounter"/>
			</Encounter>
		</Encounters>
	</xsl:template>
	
	<xsl:template match="*" mode="DefaultEncounterDetail">
		<!--
			Field : Encompassing Encounter Author
			Target: HS.SDA3.Encounter EnteredBy
			Target: /Container/Encounters/Encounter/EnteredBy
			Source: /ClinicalDocument/author
			StructuredMappingRef: EnteredByDetail
			Note  : When importing CDA encompassingEncounter to SDA
					Encounter, CDA document-level author is imported
					to SDA Encounter EnteredBy.
		-->
		<xsl:apply-templates select="$defaultAuthorRootPath" mode="EnteredBy"/>
		
		<!--
			Field : Encompassing Encounter Information Source
			Target: HS.SDA3.Encounter EnteredAt
			Target: /Container/Encounters/Encounter/EnteredAt
			Source: /ClinicalDocument/informant
			StructuredMappingRef: EnteredAt
			Note  : When importing CDA encompassingEncounter to SDA
					Encounter, CDA document-level informant is imported
					to SDA Encounter EnteredAt.
		-->
		<xsl:apply-templates select="$defaultAuthorRootPath" mode="EnteredAt"/>
		
		<!--
			Field : Encompassing Encounter Author Time
			Target: HS.SDA3.Encounter EnteredOn
			Target: /Container/Encounters/Encounter/EnteredOn
			Source: /ClinicalDocument/author/time/@value
			Note  : When importing CDA encompassingEncounter to SDA
					Encounter, CDA document-level author/time is
					imported to SDA Encounter EnteredOn.
		-->
		<xsl:apply-templates select="$defaultAuthorRootPath[1]/hl7:time" mode="EnteredOn"/>
		
		<!--
			Field : Encompassing Encounter Number
			Target: HS.SDA3.Encounter ExternalId
			Target: /Container/Encounters/Encounter/ExternalId
			Source: /ClinicalDocument/componentOf/encompassingEncounter/id[1]
			StructuredMappingRef: ExternalId
		-->
		<xsl:apply-templates select="." mode="ExternalId"/>
		
		
		<!--
			Field : Encompassing Encounter Start Date/Time
			Target: HS.SDA3.Encounter FromTime
			Target: /Container/Encounters/Encounter/FromTime
			Source: /ClinicalDocument/componentOf/encompassingEncounter/effectiveTime/low/@value
		-->
		<!--
			Field : Encompassing Encounter End Date/Time
			Target: HS.SDA3.Encounter ToTime
			Target: /Container/Encounters/Encounter/ToTime
			Source: /ClinicalDocument/componentOf/encompassingEncounter/effectiveTime/high/@value
		-->
		<xsl:variable name="fromTime">
			<xsl:choose>
				<xsl:when test="hl7:effectiveTime/hl7:low/@value">
					<xsl:value-of select="isc:evaluate('xmltimestamp', hl7:effectiveTime/hl7:low/@value)"/>
				</xsl:when>
				<xsl:when test="not(hl7:effectiveTime/hl7:low/@value) and not(hl7:effectiveTime/hl7:high/@value) and hl7:effectiveTime/@value">
					<xsl:value-of select="isc:evaluate('xmltimestamp', hl7:effectiveTime/@value)"/>
				</xsl:when>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:variable name="toTime">
			<xsl:choose>
				<xsl:when test="hl7:effectiveTime/hl7:high/@value">
					<xsl:value-of select="isc:evaluate('xmltimestamp', hl7:effectiveTime/hl7:high/@value)"/>
				</xsl:when>
				<xsl:when test="not(hl7:effectiveTime/hl7:low/@value) and not(hl7:effectiveTime/hl7:high/@value) and hl7:effectiveTime/@value">
					<xsl:value-of select="isc:evaluate('xmltimestamp', hl7:effectiveTime/@value)"/>
				</xsl:when>
			</xsl:choose>
		</xsl:variable>
		
		<!-- The logic for endTime here matches that in HS.SDA3.Streamlet.Encounter. -->
		<xsl:variable name="endTime">
			<xsl:choose>
				<xsl:when test="string-length($toTime)"><xsl:value-of select="$toTime"/></xsl:when>
				<xsl:when test="$encounterType='I'"><xsl:value-of select="'&#34;&#34;'"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="$fromTime"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:if test="string-length($fromTime)"><FromTime><xsl:value-of select="$fromTime"/></FromTime></xsl:if>
		<xsl:if test="string-length($toTime)"><ToTime><xsl:value-of select="$toTime"/></ToTime></xsl:if>
		<xsl:if test="string-length($endTime)"><EndTime><xsl:value-of select="$endTime"/></EndTime></xsl:if>
				
		<xsl:apply-templates select="hl7:priorityCode" mode="AdmissionType"/>
		
		<!--
			Field : Encompassing Encounter Admitting Clinician
			Target: HS.SDA3.Encounter AdmittingClinician
			Target: /Container/Encounters/Encounter/AdmittingClinician
			Source: /ClinicalDocument/componentOf/encompassingEncounter/encounterParticipant[@typeCode = 'ADM']
			StructuredMappingRef: DoctorDetail
		-->
		<xsl:apply-templates select="hl7:encounterParticipant[@typeCode = 'ADM']" mode="AdmittingClinician"/>
		
		<!--
			Field : Encompassing Encounter Attending Clinicians
			Target: HS.SDA3.Encounter AttendingClinicians
			Target: /Container/Encounters/Encounter/AttendingClinicians
			Source: /ClinicalDocument/componentOf/encompassingEncounter/encounterParticipant[@typeCode = 'ATND']
			StructuredMappingRef: AttendingClinicians-NoFunction
		-->
		<xsl:apply-templates select="." mode="AttendingClinicians"/>
		
		<!--
			Field : Encompassing Encounter Referring Clinician
			Target: HS.SDA3.Encounter ReferringClinician
			Target: /Container/Encounters/Encounter/ReferringClinician
			Source: /ClinicalDocument/componentOf/encompassingEncounter/encounterParticipant[@typeCode = 'REF']
			StructuredMappingRef: DoctorDetail
		-->
		<xsl:apply-templates select="hl7:encounterParticipant[@typeCode = 'REF']" mode="ReferringClinician"/>
		
		<!--
			Field : Encompassing Encounter Consulting Clinicians
			Target: HS.SDA3.Encounter ConsultingClinicians
			Target: /Container/Encounters/Encounter/ConsultingClinicians
			Source: /ClinicalDocument/componentOf/encompassingEncounter/encounterParticipant[@typeCode = 'CON']
			StructuredMappingRef: ConsultingClinicians-NoFunction
		-->
		<xsl:apply-templates select="." mode="ConsultingClinicians"/>
		
		<!--
			Field : 就诊号；就诊类型
			Target: HS.SDA3.Encounter EncounterNumber； EncounterType
			Target: /Container/Encounters/Encounter/EncounterNumber；/Container/Encounters/Encounter/EncounterType
			Source: /ClinicalDocument/recordTarget/patientRole/id/@extension
			Note  : 需要根据@root 判断就诊类型和获取就诊号( 门（急）诊：root="2.16.156.10011.1.11"，住院：root="2.16.156.10011.1.12").
		-->
		<xsl:apply-templates select="." mode="HCCEncounterNoType"/>
				
		<!--
			Field : 入院途径
			Target: HS.SDA3.Encounter AdmissionSource
			Target: /Container/Encounters/Encounter/AdmissionSource
			Source: /ClinicalDocument/componentOf/encompassingEncounter/code
			StructuredMappingRef: CodeTableDetail
			Note  : .
		<xsl:apply-templates select="hl7:code" mode="EncounterAdmissionSource"/>
		-->

		
		<!--
			Field : Encompassing Encounter MRN
			Target: HS.SDA3.Encounter EncounterMRN
			Target: /Container/Encounters/Encounter/EncounterMRN
			Source: /ClinicalDocument/recordTarget/patientRole/id[not(@root='2.16.840.1.113883.4.1')]/@extension
		-->
		<xsl:apply-templates select="." mode="EncounterMRN"/>
		
		<!--科室&病区&病房&病床-->
		<xsl:apply-templates select="hl7:location/hl7:healthCareFacility/hl7:serviceProviderOrganization/hl7:asOrganizationPartOf/hl7:wholeOrganization" mode="HCCLocation"/>
		
		
		<!--
			If encompassingEncounter has hl7:location, then use the HealthCareFacility
			template to use hl7:location as the source for SDA3 HealthCareFacility/Organization.
			Otherwise, use the encounter-HealthCareFacility template to use either hl7:id/@root
			or the derived SendingFacility code as the source.
			
			儘管HCC文檔有location，但它可能不包含醫院（机构）信息，因此需要從“文档生成机构”獲取机构信息
		-->
		<!--<xsl:choose>
			<xsl:when test="hl7:location">
				<xsl:apply-templates select="hl7:location" mode="HealthCareFacility"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="." mode="encounter-HealthCareFacility"/>
			</xsl:otherwise>
		</xsl:choose>-->
		<xsl:apply-templates select="." mode="HCC-HealthCareFacility"/>
	</xsl:template>
	
	<!-- HCC 机构(HealthCareFacility)  -->
	<xsl:template match="*" mode="HCC-HealthCareFacility">
		<HealthCareFacility>
			<Code><xsl:value-of select="/hl7:ClinicalDocument/hl7:custodian/hl7:assignedCustodian/hl7:representedCustodianOrganization/hl7:id/@extension"/></Code>
			<Description><xsl:value-of select="/hl7:ClinicalDocument/hl7:custodian/hl7:assignedCustodian/hl7:representedCustodianOrganization/hl7:name"/></Description>
        </HealthCareFacility>
	</xsl:template>
	
	
	<!-- HCC Visit Description 诊疗过程 -->
	<xsl:template match="*" mode="HCCVisitDescription">
		<VisitDescription>
			<xsl:value-of select="hl7:value"/>
		</VisitDescription>
	</xsl:template>
	
	<!-- HCC Visit Reasion -->
	<xsl:template match="*" mode="HCCVisitReason">
		<AdmitReason>
			<Code><xsl:value-of select="hl7:value"/></Code>
			<Description><xsl:value-of select="hl7:value"/></Description>
		</AdmitReason>
	</xsl:template>
	
	<!--	<xsl:template match="hl7:code" mode="EncounterAdmissionSource">
		<xsl:apply-templates select="." mode="CodeTable">
			<xsl:with-param name="hsElementName" select="'AdmissionSource'"/>
			<xsl:with-param name="importOriginalText" select="'1'"/>
		</xsl:apply-templates>
	</xsl:template>-->
	
	<!-- HCC入院途径 -->
	<xsl:template match="*" mode="EncounterAdmissionSource">
		<AdmissionSource>
			<Code><xsl:value-of select="hl7:value/@code"/></Code>
			<Description><xsl:value-of select="hl7:value/@displayName"/></Description>
		</AdmissionSource>
	</xsl:template>
	
	<!-- HCC 离院方式 -->
	<xsl:template match="*" mode="HCCASeparationMode">
		<SeparationMode>
			<Code><xsl:value-of select="hl7:value/@code"/></Code>
			<Description><xsl:value-of select="hl7:value/@displayName"/></Description>
		</SeparationMode>
	</xsl:template>
	
	
	<!--
		EncounterCodedType imports CDA coded encounter
		type information as an SDA coded element.
	
	<xsl:template match="hl7:code" mode="EncounterCodedType">
		<xsl:apply-templates select="." mode="CodeTable">
			<xsl:with-param name="hsElementName" select="'EncounterCodedType'"/>
			<xsl:with-param name="importOriginalText" select="'1'"/>
		</xsl:apply-templates>
	</xsl:template>
	-->
	
	<xsl:template match="*" mode="AttendingCliniciansDuplicate">
		<xsl:param name="overriddenEncounterRootPath"/>
		<xsl:param name="encompassingEncounterRootPath"/>
		
		<!--
			AttendingCliniciansDuplicate de-duplicates the attending
			clinicians between the encompassingEncounter and the
			Encounters section encounter, based on assignedEntity/id.
		-->
		<xsl:if test="$encompassingEncounterRootPath/hl7:encounterParticipant[@typeCode = 'ATND'] | $overriddenEncounterRootPath/hl7:performer[@typeCode = 'PRF']">
			<xsl:variable name="encompassingEncounterAttendingsIds">
				<xsl:for-each select="$encompassingEncounterRootPath/hl7:encounterParticipant[@typeCode='ATND']/hl7:assignedEntity">
					<xsl:value-of select="concat('|',hl7:id/@root,'/',hl7:id/@extension,'|')"/>
				</xsl:for-each>
			</xsl:variable>
			
			<AttendingClinicians>
				<xsl:apply-templates select="$encompassingEncounterRootPath/hl7:encounterParticipant[@typeCode = 'ATND']" mode="CareProvider"/>
				<xsl:apply-templates select="$overriddenEncounterRootPath/hl7:performer[@typeCode = 'PRF' and not(string-length(hl7:assignedEntity/hl7:id/@root) and contains($encompassingEncounterAttendingsIds,concat('|',hl7:assignedEntity/hl7:id/@root,'/',hl7:assignedEntity/hl7:id/@extension,'|')))]" mode="CareProvider"/>
			</AttendingClinicians>
		</xsl:if>
	</xsl:template>

	<xsl:template match="*" mode="EncounterMRN">
		<EncounterMRN>
			<!-- 2.16.840.1.113883.4.1 is the SSN OID -->
			<xsl:value-of select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[not(@root='2.16.840.1.113883.4.1')]/@extension"/>
		</EncounterMRN>
	</xsl:template>
	
	<xsl:template match="*" mode="EncounterMRNDuplicate">
		<xsl:param name="overriddenEncounterRootPath"/>
		<xsl:param name="encompassingEncounterRootPath"/>
		
		<!-- HS.SDA3.Encounter EncounterMRN -->

		<!--
			EncounterMRNDuplicate checks the encompassingEncounter for
			the encounter-level MRN first, then checks the Encounters
			section encounter if necessary.  If not in either then gets
			EncounterMRN from the patient-level MRN.
		-->
		<EncounterMRN>
			<xsl:choose>
				<xsl:when test="string-length($encompassingEncounterRootPath/hl7:encounterParticipant/hl7:assignedEntity/sdtc:patient/sdtc:id/@extension)">
					<xsl:value-of select="$encompassingEncounterRootPath/hl7:encounterParticipant/hl7:assignedEntity/sdtc:patient/sdtc:id/@extension"/>
				</xsl:when>
				<xsl:when test="string-length($overriddenEncounterRootPath/hl7:informant/hl7:assignedEntity/sdtc:patient/sdtc:id/@extension)">
					<xsl:value-of select="$overriddenEncounterRootPath/hl7:informant/hl7:assignedEntity/sdtc:patient/sdtc:id/@extension"/>
				</xsl:when>
				<!-- 2.16.840.1.113883.4.1 is the SSN OID -->
				<xsl:otherwise>
					<xsl:value-of select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[not(@root='2.16.840.1.113883.4.1')]/@extension"/>
				</xsl:otherwise>
			</xsl:choose>
		</EncounterMRN>
	</xsl:template>
		
	<!--HCC 获得就诊号和就诊类型-->
	<xsl:template match="*" mode="HCCEncounterNoType">
		<xsl:variable name="inpatientNo" select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.12']/@extension"/>
		<xsl:variable name="outpatientNo" select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.11']/@extension"/>
		<xsl:choose>
			<xsl:when test="string-length($inpatientNo)">
				<EncounterType><xsl:value-of select="'I'"/></EncounterType>
				<!--<EncounterNumber><xsl:value-of select="$inpatientNo"/></EncounterNumber>-->
			</xsl:when>
			<xsl:otherwise>
				<EncounterType><xsl:value-of select="'O'"/></EncounterType>
				<!--<EncounterNumber><xsl:value-of select="$outpatientNo"/></EncounterNumber>-->
			</xsl:otherwise>
		</xsl:choose>
		<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
	</xsl:template>
	
	
	<xsl:template match="*" mode="AdmissionType">
		<xsl:apply-templates select="." mode="CodeTable">
			<xsl:with-param name="hsElementName" select="'AdmissionType'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 遍历location/healthCareFacility/serviceProviderOrganization/wholeOrganization-->
	<xsl:template match="*" mode="HCCLocation">
		<xsl:choose>
			<xsl:when test="hl7:id/@root='2.16.156.10011.1.22'">
				<AssignedBed><xsl:value-of select="hl7:name"/></AssignedBed>
			</xsl:when>
			<xsl:when test="hl7:id/@root='2.16.156.10011.1.21'">
				<AssignedRoom><xsl:value-of select="hl7:name"/></AssignedRoom>
			</xsl:when>
			<xsl:when test="hl7:id/@root='2.16.156.10011.1.26'">
				<Locations><Location><LocationDetail><Code><xsl:value-of select="hl7:id/@extension"/></Code><Description><xsl:value-of select="hl7:name"/></Description></LocationDetail></Location></Locations>
				<DischargeLocation>
					<Code><xsl:value-of select="hl7:id/@extension"/></Code>
					<Description><xsl:value-of select="hl7:name"/></Description>
				</DischargeLocation>
			</xsl:when>
			<xsl:when test="hl7:id/@root='2.16.156.10011.1.27'">
				<AssignedWard><xsl:value-of select="hl7:name"/></AssignedWard>
			</xsl:when>
		</xsl:choose>
		<xsl:if test="child::*"	>	    
			<xsl:for-each select="child::*">
				<xsl:if test="local-name()='asOrganizationPartOf'"><xsl:apply-templates select="hl7:wholeOrganization" mode="HCCLocation"/></xsl:if>
			</xsl:for-each>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="*" mode="encounter-HealthCareFacility">
		<!-- 
			CDA to SDA import of duplicated encounter checks
			encompassingEncounter/location first.  If not
			found there, it instead uses
			encounter/participant[@typeCode='LOC']/participantRole[@classCode='SDLOC'].
		-->
		<!--
			$organizationCode really should get a value. If it
			does not, then that indicates something wrong with
			the document itself since not even SendingFacility
			could be found.
		-->
		<xsl:variable name="organizationCode">
			<xsl:apply-templates select="." mode="encounter-HealthCareFacility-OrganizationCode"/>
		</xsl:variable>
		
		<!--
			If location is present, then import HealthCareFacility Code,
			LocationType (if present), and Organization Code.
		-->
		<xsl:choose>
			<xsl:when test="hl7:participant[@typeCode='LOC']/hl7:participantRole[@classCode='SDLOC']">
				<HealthCareFacility>
					<Code>
						<xsl:apply-templates select="hl7:participant[@typeCode='LOC']/hl7:participantRole[@classCode='SDLOC']" mode="encounter-HealthCareFacility-LocationCode"/>
					</Code>
					
					<xsl:if test="string-length($organizationCode)">
						<Organization><Code><xsl:value-of select="$organizationCode"/></Code></Organization>
					</xsl:if>
					
					<!-- Code system 2.16.840.1.113883.6.259 is HL7 Healthcare Service Location. -->
					<xsl:variable name="locationTypeCode">
						<xsl:choose>
							<xsl:when test="hl7:code/@codeSystem='2.16.840.1.113883.6.259'">
								<xsl:choose>
									<xsl:when test="hl7:code/@code='1117-1'">CLINIC</xsl:when>
									<xsl:when test="hl7:code/@code='1160-1'">CLINIC</xsl:when>
									<xsl:when test="hl7:code/@code='1108-0'">ER</xsl:when>
									<xsl:when test="hl7:code/@code='1010-8'">DEPARTMENT</xsl:when>
									<xsl:otherwise>OTHER</xsl:otherwise>
								</xsl:choose>
							</xsl:when>
							<xsl:when test="contains('|CLINIC|ER|DEPARTMENT|WARD|OTHER|', concat('|',hl7:code/@code,'|'))">
								<xsl:value-of select="hl7:code/@code"/>
							</xsl:when>
						</xsl:choose>
					</xsl:variable>
					<xsl:if test="string-length($locationTypeCode)">
						<LocationType><xsl:value-of select="$locationTypeCode"/></LocationType>
					</xsl:if>
				</HealthCareFacility>
			</xsl:when>
			<!--
				Otherwise if $organizationCode has a value then use it
				for both Organization Code and HealthCareFacility Code.
			-->
			<xsl:when test="string-length($organizationCode)">
				<HealthCareFacility>
					<Code><xsl:value-of select="$organizationCode"/></Code>
					<Organization><Code><xsl:value-of select="$organizationCode"/></Code></Organization>
				</HealthCareFacility>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*" mode="encounter-HealthCareFacility-LocationCode">
		<xsl:choose>
			<xsl:when test="string-length(hl7:id/@extension)">
				<xsl:value-of select="hl7:id/@extension"/>
			</xsl:when>
			<xsl:when test="string-length(hl7:id/@root)">
				<xsl:apply-templates select="." mode="code-for-oid"><xsl:with-param name="OID" select="hl7:id/@root"/></xsl:apply-templates>
			</xsl:when>
			<xsl:when test="string-length(hl7:playingEntity/hl7:name/text())">
				<xsl:value-of select="hl7:playingEntity/hl7:name/text()"/>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*" mode="encounter-HealthCareFacility-OrganizationCode">
		<!--
			If hl7:id/@root is present, then it is used as the
			source for Organization Code, regardless of whether
			it has an IdentityCode defined in the OID Registry.
			If there is an IdentityCode defined, then that code
			is used.  Otherwise, the @root is used as is.
			
			If hl7:id/@root is not present, then use the derived
			SendingFacility value as the source for Organization
			Code.
		-->
		<xsl:variable name="organizationCodeFromIdRoot">
			<xsl:apply-templates select="." mode="code-for-oid"><xsl:with-param name="OID" select="hl7:id/@root"/></xsl:apply-templates>
		</xsl:variable>
		
		<xsl:variable name="sendingFacilityForEncounter"><xsl:apply-templates select="/hl7:ClinicalDocument" mode="SendingFacilityValue"/></xsl:variable>
		
		<xsl:choose>
			<xsl:when test="string-length($organizationCodeFromIdRoot)">
				<xsl:value-of select="$organizationCodeFromIdRoot"/>
			</xsl:when>
			<xsl:when test="string-length($sendingFacilityForEncounter)">
				<xsl:value-of select="$sendingFacilityForEncounter"/>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*" mode="ConsultingClinicians-informant">
		<xsl:if test="hl7:informant[not(hl7:assignedEntity/hl7:id/@nullFlavor) and hl7:assignedEntity/hl7:id/@root and hl7:assignedEntity/hl7:id/@extension and string-length(hl7:assignedEntity/hl7:assignedPerson/hl7:name)]">
			<ConsultingClinicians>
				<xsl:apply-templates select="hl7:informant[not(hl7:assignedEntity/hl7:id/@nullFlavor) and hl7:assignedEntity/hl7:id/@root and hl7:assignedEntity/hl7:id/@extension and string-length(hl7:assignedEntity/hl7:assignedPerson/hl7:name)]" mode="CareProvider"/>
			</ConsultingClinicians>
		</xsl:if>
	</xsl:template>
	
	<!--
		This empty template may be overridden with custom logic.
	
		The input node spec can be either of the following:
		- /hl7:ClinicalDocument/hl7:componentOf/hl7:encompassingEncounter
		- $sectionRootPath/hl7:entry/hl7:encounter
	-->
	<xsl:template match="*" mode="ImportCustom-Encounter">
	</xsl:template>
	
	<!--
		Determine if the Payers section is present but has or indicates no data present.
		This logic is applied only if the section is present.
		The input node spec is $planSection.
		Return 1 if the section is present and there is no hl7:entry element.
		Otherwise Return 0 (section is present and appears to include payers data).
		
		You may override this template to use custom criteria to determine "No Data" section.
	-->
	<xsl:template match="*" mode="IsNoDataSection-Payers">
		<xsl:choose>
			<xsl:when test="count(hl7:entry)=0">1</xsl:when>
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
